#include "USBClientConnector.h"
#include "soft_usb.h"
#include <Arduino.h>

#ifndef _USB_Manager_H
#define _USB_Manager_H

struct USBMessage;

#if !defined USE_NATIVE_GROUP_TIMERS

typedef struct
{
    int type; // the type of timer's event
    int timer_group;
    int timer_idx;
    uint64_t timer_counter_value;
} timer_event_t;
static xQueueHandle timer_queue = NULL;

#endif

typedef void (*OnConnect_t)(uint8_t usbNum, bool &bFlag);
typedef void (*OnDisonnect_t)(uint8_t usbNum);

class USBManager {
  private:
    // 所有USB设备
    static USBClientConnector s_usbConnectors[2]; // 连接池

    void (*onConnectCallback)(uint8_t usbNum, bool &bFlag) = nullptr;
    void (*onDisconnectCallback)(uint8_t usbNum) = nullptr;

    void OnDevDesc(uint8_t usbNum, uint8_t *data, size_t data_len);
    void OnConfigDescCB(uint8_t usbNum, int cfgNum, uint8_t *pData, size_t len);
    void OnIFaceDesc(uint8_t usbNum, int cfgNum, int sIntfNum, uint8_t *data, size_t len);
    void OnHidDevDesc(uint8_t usbNum, int cfgNum, int sIntfNum, int hidNum, uint8_t *vhid, size_t len);
    void OnEndPointDesc(uint8_t usbNum, int cfgNum, int sIntfNum, int epdNUm, uint8_t *data, size_t len);

  public:
    void init(int DP0, int DM0, int DP1, int DM1, int DP2, int DM2, int DP3, int DM3);

    void setOnConnect(OnConnect_t cd);
    void setOnDisonnect(OnDisonnect_t cd);

    static USBClientConnector *getUSBClientConnector(uint8_t usbNum);

    static void UsbOnDetect(uint8_t usbNum, sDevDesc *devDesc, size_t len);
    static void UsbOnEject(uint8_t usbNum);

    static void UsbOnError(uint8_t usbNum, int errorCode);
    static void UsbOnRecvData(uint8_t usbNum, uint8_t *data, size_t data_len);
};

extern USBManager _USBManager;

#endif
